    \documentclass{article}
    \usepackage[separator={{ }}]{datatool}
    \usepackage{pgfplots}
    \def\whattodraw{}
    \newif\ifpatchagain
    
    %\begin{filecontents}{dataset.dat}
    %running 20
    %stage-out 47
    %running 34
    %stage-in 3
    %running 24
    %stage-out 39
    %\end{filecontents}
    %
    %\begin{filecontents}{datasetcolor.dat}
    %stage-in red!75
    %stage-out green
    %running orange
    %\end{filecontents}
    
    \DTLloaddb[noheader,keys={name,value},headers={name,value}]{dataset}{tl.txt}
    \DTLloaddb[noheader,keys={name,color},headers={name,color}]{datasetcolor}{colors.txt}
    
    \DTLforeach{datasetcolor}{\name=name,\tikzColor=color}{
        \tikzset{DSCOLOR\name/.estyle={fill=\tikzColor}}
    }
    
    \DTLforeach*{dataset}{\namea=name,\vala=value}{%
    	\eappto\whattodraw{%
    		QQQreplacemeQQQ[
    			scatter,
    			DSCOLOR\namea,
    			no marks,
    			draw=none,
    			scatter src=explicit symbolic
    		] coordinates {(\vala,0) [\namea]};
    	}
    }
    \loop
    \ifpatchable{\whattodraw}{QQQreplacemeQQQ}{\patchagaintrue}{\patchagainfalse}
    \ifpatchagain
    \patchcmd{\whattodraw}{QQQreplacemeQQQ}{\addplot}{}{}
    \repeat
    
    \begin{document}
    \ref{legendhere}
    
    \begin{tikzpicture}
    \begin{axis}[
        xmin=0,
        axis y line=none,
        axis x line=bottom,
        xbar stacked,
        stack dir=plus,
        height=5cm,
        legend columns=-1,
        width=\textwidth,
        legend to name={legendhere},
        scatter/classes={
            stage-in={red!75},%
            running={orange},%
            stage-out={green}}
    ]
    \whattodraw
    \legend{stage-in,running,stage-out}
    \end{axis}
    \end{tikzpicture}
    \end{document}

